package org.aom.litebean.module.security;

import java.util.Map;

import org.aom.entity.security.User;
import org.aom.litebean.module.base.BaseEntityFormBean;
import org.aom.service.security.IUserManager;
import org.aom.util.FaceUtil;
import org.aom.util.MD5Util;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.annotation.Required;
import org.operamasks.faces.annotation.ValidateRegexp;
import org.operamasks.faces.validator.CommonRegexpPatterns;

@ManagedBean(name = "module.security.userFormBean", scope = ManagedBeanScope.REQUEST)
public class UserFormBean extends BaseEntityFormBean {

	@Bind(id = "loginName", attribute = "value")
	@Required(message = "#{this.getMessages().get('required')}")
	private String loginName;

	@Bind(id = "password", attribute = "value")
	@Required(message = "#{this.getMessages().get('required')}")
	private String password;

	@Bind(id = "name", attribute = "value")
	@Required(message = "#{this.getMessages().get('required')}")
	private String name;
	
	@Bind(id = "userState", attribute = "value")
	@Required(message = "#{this.getMessages().get('required')}")
	private String userState;

	@Bind(id = "email", attribute = "value")
	@Required(message = "#{this.getMessages().get('required')}")
	@ValidateRegexp(value = CommonRegexpPatterns.EMAIL_ADDRESS, message = "邮件格式错误")
	private String email;

	@ManagedProperty("#{userManager}")
	private IUserManager userManager;

	private User user;

	@Override
	@Action(id = "ok")
	protected void ok() {
		Map<String, Object> session = FaceUtil.getSessionMap();
		if (session.get(ACTION_NAME).toString().equals("add")) {
			user = new User();
		} else {
			selectedModuleId = (Long) session.get(SELECTED_MODULE_ID);
			user = userManager.get(selectedModuleId);
		}
		user.setLoginName(loginName);
		user.setName(name);
		String changePassword = password.trim();
		user.setPassword(MD5Util.getMd5PasswordEncoder(changePassword, loginName));//喜伟说这里只能用登陆名作为salt，否则无法登陆，为啥子呢？
		user.setEnable(Integer.parseInt(userState));
		user.setEmail(email);
		user.setExpTime(0L);
		userManager.save(user);
		cancel();
	}

	@Override
	protected void processCancel() {
		loginName = "";
		name = "";
		password = "";
		email = "";
	}

	@BeforeRender
	protected void beforeRender(boolean isPostBack) {
		initBaseResource();
		Map<String, Object> session = FaceUtil.getSessionMap();
		if (session.get(ACTION_NAME).toString().equals("mod")) {
			selectedModuleId = (Long) session.get(SELECTED_MODULE_ID);
			user = userManager.get(selectedModuleId);
			loginName = user.getLoginName();
			password = user.getPassword();
			userState = ((Integer)user.getEnable()).toString();
			name = user.getName();
			email = user.getEmail();
		}
	}

}
